package time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class JDK8Local {
    public static void main(String[] args) {
        //获取当前日期对象
        LocalDate nowDate = LocalDate.now();
        //获取当前时间对象
        LocalTime nowTime = LocalTime.now();
        //获取当前日期时间对象
        LocalDateTime nowDateTime = LocalDateTime.now();

        System.out.println("当前日期："+nowDate);
        System.out.println("当前时间："+nowTime);
        System.out.println("当前日期时间："+nowDateTime);

        //设置指定的日期时间
        LocalDateTime of = LocalDateTime.of(2021, 7, 26, 19, 32);
        System.out.println(of);

        //获取当前时间的相关属性
        int dayOfMonth = nowDateTime.getDayOfMonth();
        System.out.println("当前（指定）日期是这个月的第几天:"+dayOfMonth);

        System.out.println("当前（指定）日期是星期几:"+nowDateTime.getDayOfWeek());

        System.out.println("当前（指定）日期是几月份（英文）:"+nowDate.getMonth());

        //体现不可变性——当设置当前时间属性时，原来的日期对象不受影响
        LocalDateTime localDateTime = nowDateTime.withDayOfMonth(22);
        System.out.println("原时间对象:"+nowDateTime);
        System.out.println(localDateTime);


        System.out.println("格式化+++++");

        /*
        * 格式化输出
        * */
        //一：默认格式
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime now = LocalDateTime.now();
        String format = formatter.format(now);
        System.out.println(format);

        //二、其他格式
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        System.out.println(formatter1.format(now));


        //三、自定义
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh-mm-ss");
        String format2 = formatter2.format(now);
        System.out.println("自定义格式:"+format2);

        System.out.println("解析+++++");
        /*
        * 解析
        * */
        System.out.println("默认格式日期解析:"+formatter.parse(format));
        System.out.println("自定义日期解析:"+formatter2.parse(format2));

    }
}
